﻿
namespace bookMaster.AssetSystem.Windows.Forms
{
	/// <summary>
	/// abstract UsuariosRecursos typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class UsuariosRecursosDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<UsuariosRecursosDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.UsuariosRecursos _currentUsuariosRecursos = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxUsuariosRecursosDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxUsuariosRecursosErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxUsuariosRecursosBindingSource;
		
		/// <summary> 
		/// the DGV column associated with the IdUsuario property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIdUsuarioDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IdRecurso property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIdRecursoDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the PermiteAcceso property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxPermiteAccesoDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the PermiteEscritura property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxPermiteEscrituraDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the PermisosEspeciales property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxPermisosEspecialesDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.Recursos> _IdRecursoList;
		
		/// <summary> 
		/// The list of selectable Recursos
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.Recursos> IdRecursoList
		{
			get {return this._IdRecursoList;}
			set 
			{
				this._IdRecursoList = value;
				this.uxIdRecursoDataGridViewColumn.DataSource = null;
				this.uxIdRecursoDataGridViewColumn.DataSource = this._IdRecursoList;
			}
		}
		
		private bool _allowNewItemInIdRecursoList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of Recursos
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIdRecursoList
		{
			get { return _allowNewItemInIdRecursoList;}
			set
			{
				this._allowNewItemInIdRecursoList = value;
				this.uxIdRecursoDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
				
		private Entities.TList<Entities.Usuarios> _IdUsuarioList;
		
		/// <summary> 
		/// The list of selectable Usuarios
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.Usuarios> IdUsuarioList
		{
			get {return this._IdUsuarioList;}
			set 
			{
				this._IdUsuarioList = value;
				this.uxIdUsuarioDataGridViewColumn.DataSource = null;
				this.uxIdUsuarioDataGridViewColumn.DataSource = this._IdUsuarioList;
			}
		}
		
		private bool _allowNewItemInIdUsuarioList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of Usuarios
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIdUsuarioList
		{
			get { return _allowNewItemInIdUsuarioList;}
			set
			{
				this._allowNewItemInIdUsuarioList = value;
				this.uxIdUsuarioDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.UsuariosRecursos> _UsuariosRecursosList;
				
		/// <summary> 
		/// The list of UsuariosRecursos to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.UsuariosRecursos> UsuariosRecursosList
		{
			get {return this._UsuariosRecursosList;}
			set
			{
				this._UsuariosRecursosList = value;
				this.uxUsuariosRecursosBindingSource.DataSource = null;
				this.uxUsuariosRecursosBindingSource.DataSource = value;
				this.uxUsuariosRecursosDataGridView.DataSource = null;
				this.uxUsuariosRecursosDataGridView.DataSource = this.uxUsuariosRecursosBindingSource;				
				//this.uxUsuariosRecursosBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxUsuariosRecursosBindingSource_ListChanged);
				this.uxUsuariosRecursosBindingSource.CurrentItemChanged += new System.EventHandler(OnUsuariosRecursosBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnUsuariosRecursosBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentUsuariosRecursos = uxUsuariosRecursosBindingSource.Current as Entities.UsuariosRecursos;
			
			if (_currentUsuariosRecursos != null)
			{
				_currentUsuariosRecursos.Validate();
			}
			//_UsuariosRecursos.Validate();
			OnCurrentEntityChanged();
		}

		//void uxUsuariosRecursosBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.UsuariosRecursos"/> instance.
		/// </summary>
		public Entities.UsuariosRecursos SelectedUsuariosRecursos
		{
			get {return this._currentUsuariosRecursos;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxUsuariosRecursosDataGridView.VirtualMode;}
			set
			{
				this.uxUsuariosRecursosDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxUsuariosRecursosDataGridView.AllowUserToAddRows;}
			set {this.uxUsuariosRecursosDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxUsuariosRecursosDataGridView.AllowUserToDeleteRows;}
			set {this.uxUsuariosRecursosDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxUsuariosRecursosDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxUsuariosRecursosDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="UsuariosRecursosDataGridViewBase"/> class.
		/// </summary>
		public UsuariosRecursosDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxUsuariosRecursosDataGridView = new System.Windows.Forms.DataGridView();
			this.uxUsuariosRecursosBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxUsuariosRecursosErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIdUsuarioDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxIdRecursoDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxPermiteAccesoDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxPermiteEscrituraDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxPermisosEspecialesDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			//this.uxIdRecursoBindingSource = new RecursosBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIdRecursoBindingSource)).BeginInit();
			//this.uxIdUsuarioBindingSource = new UsuariosBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIdUsuarioBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxUsuariosRecursosDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxUsuariosRecursosBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxUsuariosRecursosErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxUsuariosRecursosErrorProvider
			// 
			this.uxUsuariosRecursosErrorProvider.ContainerControl = this;
			this.uxUsuariosRecursosErrorProvider.DataSource = this.uxUsuariosRecursosBindingSource;						
			// 
			// uxUsuariosRecursosDataGridView
			// 
			this.uxUsuariosRecursosDataGridView.AutoGenerateColumns = false;
			this.uxUsuariosRecursosDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxUsuariosRecursosDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIdUsuarioDataGridViewColumn,
		this.uxIdRecursoDataGridViewColumn,
		this.uxPermiteAccesoDataGridViewColumn,
		this.uxPermiteEscrituraDataGridViewColumn,
		this.uxPermisosEspecialesDataGridViewColumn			});
			this.uxUsuariosRecursosDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxUsuariosRecursosDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxUsuariosRecursosDataGridView.Name = "uxUsuariosRecursosDataGridView";
			this.uxUsuariosRecursosDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxUsuariosRecursosDataGridView.TabIndex = 0;	
			this.uxUsuariosRecursosDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxUsuariosRecursosDataGridView.EnableHeadersVisualStyles = false;
			this.uxUsuariosRecursosDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnUsuariosRecursosDataGridViewDataError);
			this.uxUsuariosRecursosDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnUsuariosRecursosDataGridViewCellValueNeeded);
			this.uxUsuariosRecursosDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnUsuariosRecursosDataGridViewCellValuePushed);
			
			//
			// uxIdUsuarioDataGridViewColumn
			//
			this.uxIdUsuarioDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIdUsuarioDataGridViewColumn.DataPropertyName = "IdUsuario";
			this.uxIdUsuarioDataGridViewColumn.HeaderText = "IdUsuario";
			this.uxIdUsuarioDataGridViewColumn.Name = "uxIdUsuarioDataGridViewColumn";
			this.uxIdUsuarioDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIdUsuarioDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIdUsuarioDataGridViewColumn.ReadOnly = false;		
			//
			// uxIdRecursoDataGridViewColumn
			//
			this.uxIdRecursoDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIdRecursoDataGridViewColumn.DataPropertyName = "IdRecurso";
			this.uxIdRecursoDataGridViewColumn.HeaderText = "IdRecurso";
			this.uxIdRecursoDataGridViewColumn.Name = "uxIdRecursoDataGridViewColumn";
			this.uxIdRecursoDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIdRecursoDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIdRecursoDataGridViewColumn.ReadOnly = false;		
			//
			// uxPermiteAccesoDataGridViewColumn
			//
			this.uxPermiteAccesoDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxPermiteAccesoDataGridViewColumn.DataPropertyName = "PermiteAcceso";
			this.uxPermiteAccesoDataGridViewColumn.HeaderText = "PermiteAcceso";
			this.uxPermiteAccesoDataGridViewColumn.Name = "uxPermiteAccesoDataGridViewColumn";
			this.uxPermiteAccesoDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxPermiteAccesoDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxPermiteAccesoDataGridViewColumn.ReadOnly = false;		
			//
			// uxPermiteEscrituraDataGridViewColumn
			//
			this.uxPermiteEscrituraDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxPermiteEscrituraDataGridViewColumn.DataPropertyName = "PermiteEscritura";
			this.uxPermiteEscrituraDataGridViewColumn.HeaderText = "PermiteEscritura";
			this.uxPermiteEscrituraDataGridViewColumn.Name = "uxPermiteEscrituraDataGridViewColumn";
			this.uxPermiteEscrituraDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxPermiteEscrituraDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxPermiteEscrituraDataGridViewColumn.ReadOnly = false;		
			//
			// uxPermisosEspecialesDataGridViewColumn
			//
			this.uxPermisosEspecialesDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxPermisosEspecialesDataGridViewColumn.DataPropertyName = "PermisosEspeciales";
			this.uxPermisosEspecialesDataGridViewColumn.HeaderText = "PermisosEspeciales";
			this.uxPermisosEspecialesDataGridViewColumn.Name = "uxPermisosEspecialesDataGridViewColumn";
			this.uxPermisosEspecialesDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxPermisosEspecialesDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxPermisosEspecialesDataGridViewColumn.ReadOnly = false;		
			//
			// uxIdRecursoDataGridViewColumn
			//				
			this.uxIdRecursoDataGridViewColumn.DisplayMember = "LlaveRecurso";	
			this.uxIdRecursoDataGridViewColumn.ValueMember = "IdRecurso";	
			this.uxIdRecursoDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIdRecursoDataGridViewColumn.DataSource = uxIdRecursoBindingSource;				
				
			//
			// uxIdUsuarioDataGridViewColumn
			//				
			this.uxIdUsuarioDataGridViewColumn.DisplayMember = "NombreUsuario";	
			this.uxIdUsuarioDataGridViewColumn.ValueMember = "IdUsuario";	
			this.uxIdUsuarioDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIdUsuarioDataGridViewColumn.DataSource = uxIdUsuarioBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxUsuariosRecursosDataGridView);
			this.Name = "UsuariosRecursosDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxIdRecursoBindingSource)).EndInit();
			//((System.ComponentModel.ISupportInitialize)(this.uxIdUsuarioBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxUsuariosRecursosErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxUsuariosRecursosDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxUsuariosRecursosBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				UsuariosRecursosDataGridViewEventArgs args = new UsuariosRecursosDataGridViewEventArgs();
				args.UsuariosRecursos = _currentUsuariosRecursos;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class UsuariosRecursosDataGridViewEventArgs : System.EventArgs
		{
			private Entities.UsuariosRecursos	_UsuariosRecursos;
	
			/// <summary>
			/// the  Entities.UsuariosRecursos instance.
			/// </summary>
			public Entities.UsuariosRecursos UsuariosRecursos
			{
				get { return _UsuariosRecursos; }
				set { _UsuariosRecursos = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxUsuariosRecursosDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnUsuariosRecursosDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxUsuariosRecursosDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnUsuariosRecursosDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxUsuariosRecursosDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdUsuarioDataGridViewColumn":
						e.Value = UsuariosRecursosList[e.RowIndex].IdUsuario;
						break;
					case "uxIdRecursoDataGridViewColumn":
						e.Value = UsuariosRecursosList[e.RowIndex].IdRecurso;
						break;
					case "uxPermiteAccesoDataGridViewColumn":
						e.Value = UsuariosRecursosList[e.RowIndex].PermiteAcceso;
						break;
					case "uxPermiteEscrituraDataGridViewColumn":
						e.Value = UsuariosRecursosList[e.RowIndex].PermiteEscritura;
						break;
					case "uxPermisosEspecialesDataGridViewColumn":
						e.Value = UsuariosRecursosList[e.RowIndex].PermisosEspeciales;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxUsuariosRecursosDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnUsuariosRecursosDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxUsuariosRecursosDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdUsuarioDataGridViewColumn":
						UsuariosRecursosList[e.RowIndex].IdUsuario = (string)e.Value;
						break;
					case "uxIdRecursoDataGridViewColumn":
						UsuariosRecursosList[e.RowIndex].IdRecurso = (int)e.Value;
						break;
					case "uxPermiteAccesoDataGridViewColumn":
						UsuariosRecursosList[e.RowIndex].PermiteAcceso = (short)e.Value;
						break;
					case "uxPermiteEscrituraDataGridViewColumn":
						UsuariosRecursosList[e.RowIndex].PermiteEscritura = (short)e.Value;
						break;
					case "uxPermisosEspecialesDataGridViewColumn":
						UsuariosRecursosList[e.RowIndex].PermisosEspeciales = (short)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
